home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 020 / arcsourc.arc / ARCUSQ.MAC < prev    next >
Encoding:
Text File  |  1985-06-25  |  3.0 KB  |  94 lines

  1. /*  ARC - Archive utility - ARCUSQ
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =3.04), created on $tag(
  6. TED_DATE DB =06/25/85) at $tag(
  7. TED_TIME DB =12:11:20))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to expand a file
  17.          which was packed using Huffman squeezing.
  18.  
  19.          Most of this code is taken from an USQ program by Richard
  20.          Greenlaw, which was adapted to CI-C86 by Robert J. Beilstein.
  21.  
  22.     Language:
  23.          Computer Innovations Optimizing C86
  24. */
  25. #include <stdio.h>
  26. #include "arc.h"
  27.  
  28. /* stuff for Huffman unsqueezing */
  29.  
  30. #define ERROR (-1)
  31.  
  32. #define SPEOF 256                      /* special endfile token */
  33. #define NUMVALS 257                    /* 256 data values plus SPEOF */
  34.  
  35. static struct nd                       /* decoding tree */
  36. {   int child[2];                      /* left, right */
  37. }   node[NUMVALS-1];
  38.  
  39. static int bpos;                       /* last bit position read */
  40. static int curin;                      /* last byte value read */
  41. static int numnodes;                   /* number of nodes in decode tree */
  42.  
  43. init_usq(f)                            /* initialize Huffman unsqueezing */
  44. FILE *f;                               /* file containing squeezed data */
  45. {
  46.     int i;                             /* node index */
  47.  
  48.     bpos = 99;                         /* force initial read */
  49.  
  50.     fread(&numnodes,sizeof(numnodes),1,f);
  51.  
  52.     if(numnodes<0 || numnodes>=NUMVALS)
  53.          abort("File has an invalid decode tree");
  54.  
  55.     /* initialize for possible empty tree (SPEOF only) */
  56.  
  57.     node[0].child[0] = -(SPEOF + 1);
  58.     node[0].child[1] = -(SPEOF + 1);
  59.  
  60.     for(i=0; i<numnodes; ++i)          /* get decoding tree from file */
  61.     {    fread(&node[i].child[0],sizeof(node[i].child[0]),1,f);
  62.          fread(&node[i].child[1],sizeof(node[i].child[1]),1,f);
  63.     }
  64. }
  65.  
  66. int getc_usq(f)                        /* get byte from squeezed file */
  67. FILE *f;                               /* file containing squeezed data */
  68. {
  69.     int i;                             /* tree index */
  70.  
  71.     /* follow bit stream in tree to a leaf */
  72.  
  73.     for(i=0; i>=0; )                   /* work down(up?) from root */
  74.     {    if(++bpos>7)
  75.          {    if((curin=getc_unp(f)) == ERROR)
  76.                    return(ERROR);
  77.               bpos = 0;
  78.  
  79.               /* move a level deeper in tree */
  80.               i = node[i].child[1&curin];
  81.          }
  82.          else i = node[i].child[1 & (curin >>= 1)];
  83.     }
  84.  
  85.     /* decode fake node index to original data value */
  86.  
  87.     i = -(i + 1);
  88.  
  89.     /* decode special endfile token to normal EOF */
  90.  
  91.     i = (i==SPEOF) ? EOF : i;
  92.     return i;
  93. }
  94.